package com.jy.show.controller;

import com.jy.show.bean.Province;
import com.jy.show.common.ResultCode;
import com.jy.show.common.ResultCommon;
import com.jy.show.service.PatienceService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class PatienceController {

    @Autowired
    private PatienceService patienceService;

    @PostMapping("/province")
    @ApiOperation("插入城市疫情信息")
    public ResultCommon insertProvince(Province province) {
        ResultCommon result = null;
        int i = patienceService.insertProvince(province);
        if (i == 1) {
            result = ResultCommon.success(ResultCode.SUCCESS, "插入成功");
            return result;
        } else {
            result = ResultCommon.fail(ResultCode.FAIL);
            return result;
        }
    }

    @RequestMapping("/getPatienceAll")
    @ResponseBody
    @ApiOperation(value = "查询所有疫情信息")
    public ResultCommon getPatienceAll() {
        ResultCommon result = null;
        List<Province> provinces = patienceService.queryAllPatience();
        result = ResultCommon.success(ResultCode.ALL_SUCCESS, provinces);
        return result;
    }

    @PostMapping("/updatePatience")
    @ResponseBody
    @ApiOperation("更新疫情数据")
    public ResultCommon updatePatience(Province province) {
        ResultCommon result = null;
        int count = patienceService.updatePatience(province);
        if (count >= 1) {
            result = ResultCommon.success(ResultCode.SUCCESS, "更新成功");
            return result;
        } else {
            result = ResultCommon.fail(ResultCode.FAIL);
            return result;
        }
    }

    @DeleteMapping("/province")
    @ResponseBody
    @ApiOperation("删除疫情信息")
    public ResultCommon deleteProvince(int id) {
        ResultCommon result = null;
        int i = patienceService.deleteProvince(id);
        if (i == 1) {
            result = ResultCommon.success(ResultCode.SUCCESS, "删除成功");
            return result;
        } else {
            result = ResultCommon.fail(ResultCode.FAIL);
            return result;
        }
    }

    @GetMapping("/exportExcel")
    public ResultCommon exportExcel(Integer[] id, HttpServletResponse response) throws Exception {
        ResultCommon result = null;
        //1.查询要导出的数据
        List idList = new ArrayList();
        for (int i = 0; i < id.length; i++) {
            idList.add(id[i]);
        }
        List<Province> provinces = patienceService.ExcelExport(idList);
        //2.在内存中创建一个工作簿,工作簿的名字是最后写文件的时候设置
        HSSFWorkbook wb = new HSSFWorkbook();
        //3.创建一个sheet
        HSSFSheet sheet = wb.createSheet("疫情信息");
        String[] titles = {"省份名", "确诊人数", "疑似病例", "已治愈人数", "死亡人数", "新闻提要"};
        int index = 0;
        HSSFRow row = sheet.createRow(index);
        for (int i = 0; i < titles.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(titles[i]);
        }

        //4.装载数据
        for (int i = 0; i < provinces.size(); i++) {
            Province province = provinces.get(i);
            //5.得到一行
            row = sheet.createRow(++index);

            //6.得到行里面的列,到导出数据
            HSSFCell cellHouserno = row.createCell(0);
            cellHouserno.setCellValue(province.getProvinceName());

            HSSFCell cellHouser = row.createCell(1);
            cellHouser.setCellValue(province.getConfirmedCount());

            HSSFCell cellArea = row.createCell(2);
            cellArea.setCellValue(province.getSuspectedCount());

            HSSFCell cellPhone = row.createCell(3);
            cellPhone.setCellValue(province.getCuredCount());

            HSSFCell cellDepart = row.createCell(4);
            cellDepart.setCellValue(province.getDeadCount());

            HSSFCell cellEntryCount = row.createCell(5);
            cellEntryCount.setCellValue(province.getComment());
        }

        //执行文件下载
        ServletOutputStream outputStream = response.getOutputStream();
        String filename = "疫情信息统计表" + new SimpleDateFormat("YYYYMMDDHHmmSSsss").format(new Date()) + ".xls";
        //FileOutputStream fos = new FileOutputStream(new File("E:/Desktop/"+filename));
        //强制下载不打开
        response.setContentType("application/force-download");
        //中文需要编码
        response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filename, "utf-8"));
        wb.write(outputStream);
        outputStream.flush();
        outputStream.close();
        result = ResultCommon.success(ResultCode.SUCCESS);
        return result;
    }

    @GetMapping("/getPatienceById")
    public ResultCommon getPatience(Integer id) {
        ResultCommon result = null;
        Province province = patienceService.getPatience(id);
        result = ResultCommon.success(ResultCode.LAYUI_SUCCESS, province);
        return result;
    }
}
